"""
Update LongPort Access Token
===========================

This script provides a convenient way to update the LongPort access token
in the .env file.
"""

import os
from dotenv import load_dotenv
from config_manager import ConfigManager


def update_longport_token(new_token):
    """
    Update the LongPort access token
    
    Args:
        new_token (str): New access token
    """
    config_manager = ConfigManager()
    
    # Update the access token
    updates = {
        'LONGPORT_ACCESS_TOKEN': new_token
    }
    
    config_manager.update_env_file(updates)
    
    # Reload environment variables
    config_manager.reload_env()
    
    # Validate the configuration
    if config_manager.validate_longport_config():
        print("✅ LongPort token updated successfully!")
        return True
    else:
        print("❌ Failed to update LongPort token")
        return False


def main():
    """Main function to update LongPort token"""
    print("LongPort Token Updater")
    print("=" * 30)
    
    # Example of how to use this script
    print("To update the LongPort access token, call:")
    print("update_longport_token('your_new_token_here')")
    
    # You can also manually edit the .env file
    print("\nOr manually edit the .env file:")
    print("1. Open .env file")
    print("2. Update LONGPORT_ACCESS_TOKEN value")
    print("3. Save the file")
    
    # Show current configuration
    config_manager = ConfigManager()
    config = config_manager.get_longport_config()
    
    print(f"\nCurrent configuration:")
    print(f"App Key: {config['app_key'][:10]}..." if config['app_key'] else "Not set")
    print(f"App Secret: {config['app_secret'][:10]}..." if config['app_secret'] else "Not set")
    print(f"Access Token: {config['access_token'][:20]}..." if config['access_token'] else "Not set")


if __name__ == "__main__":
    main()
